<?php
	class template {
	   private $compileDir;
       private $templateDir;
       public $variables = Array();
       
       public function setCompileDir($dir) {
        $src = substr($dir, -1);
        $src == '/' ? $this->compileDir = $dir : $this->compileDir = $dir.'/';
       }
       
       public function setTemplateDir($dir) {
        $src = substr($dir, -1);
        $src == '/' ? $this->templateDir = $dir : $this->templateDir = $dir.'/';
       }
       
       public function assign($name, $value) {
            $this->variables[$name] = $value;
       }
       
       public function assigns($value) {
        if(is_array($value)) {
            foreach($value as $key => $result) {
                self::assign($key, $result);
            }
        }
       }
       
       private function includeFile($file) {
        self::display($file);
       }
       
       private function compile($tpl_file) {
        $content = file_get_contents($this->templateDir.$tpl_file);
        if(!empty($content)) {
            
            $content = preg_replace('/{\$(\w*)}/siU', '<?php echo $this->variables[\'$1\'] ?>', $content);
            $pattern = array();
            $pattern[0] = '#{include file="(.*)"}#';
            $pattern[1] = '#{if \$(.*)\.(.*) \% ([0-9]+) \=\= ([0-9]+)}#';
            $pattern[2] = '#{if \$(.*)\.(.*) (==|!=|<|>|<=|>=) ([0-9]+)}#';
            $pattern[3] = '#{if (.*)\(\$(.*)\)}#';
            $pattern[4] = '#{if \$(.*) (==|!=|<|>|<=|>=) \$(.*)}#';
            $pattern[5] = '#{if \$(.*) (==|!=|<|>|<=|>=) ([A-Za-z0-9\_-\s\"]+)}#';
            $pattern[6] = '#{if !\$(.*)}#';
            $pattern[7] = '#{if \$(.*)}#';
            $pattern[8] = '#{elseif \$(.*) (==|!=|<|>|<=|>=) \$(.*)}#';
            $pattern[9] = '#{elseif \$(.*) (==|!=|<|>|<=|>=) ([A-Za-z0-9\_-\s\"]+)}#';
            $pattern[10] = '#{elseif !\$(.*)}#';
            $pattern[11] = '#{elseif \$(.*)}#';
            $pattern[12] = '#{else}#';
            $pattern[13] = '#{/if}#';
            $pattern[14] = '#{\$(.*)\.(.*)}#U';
            $pattern[15] = '#{foreach \$(.*) as (.*)}#';
            $pattern[16] = "#{/foreach}#";
 
            $replacement = array();
            $replacement[0] = "<?php self::includeFile('$1') ?>";
            $replacement[1] = '<?php if($$1[\'$2\'] % $3 == $4) { ?>';
            $replacement[2] = '<?php if($$1[\'$2\'] $3 $4) { ?>';
            $replacement[3] = '<?php if($1($this->variables[\'$2\'])) { ?>';
            $replacement[4] = '<?php if($this->variables[\'$1\'] $2 $this->variables[\'$3\']) { ?>';
            $replacement[5] = '<?php if($this->variables[\'$1\'] $2 $3) { ?>';
            $replacement[6] = '<?php if(!$this->variables[\'$1\']) { ?>';
            $replacement[7] = '<?php if($this->variables[\'$1\']) { ?>';
            $replacement[8] = '<?php } elseif($this->variables[\'$1\'] $2 $this->variables[\'$3\']) { ?>';
            $replacement[9] = '<?php } elseif($this->variables[\'$1\'] $2 $3) { ?>';
            $replacement[10] = '<?php } elseif(!$this->variables[\'$1\']) { ?>';
            $replacement[11] = '<?php } elseif($this->variables[\'$1\']) { ?>';
            $replacement[12] = '<?php } else { ?>';
            $replacement[13] = '<?php } ?>';
            $replacement[14] = '<?php echo $$1[\'$2\'] ?>';
            $replacement[15] = '<?php if(is_array($this->variables[\'$1\'])) { foreach($this->variables[\'$1\'] as $2) {  ?>';
            $replacement[16] = "<?php }} ?>";
        

            $content = preg_replace($pattern, $replacement, $content);
            
            $name = explode(".",$tpl_file);
            $header = '
            <?php
                $MD5_FILE_'.$name[0].' = "'.md5_file($this->templateDir.$tpl_file).'";
            ?>
            ';
            file_put_contents($this->compileDir.'template.'.$tpl_file.'.php',$header.$content);
            require_once($this->compileDir.'template.'.$tpl_file.'.php');
        } else {
            throw new Exception('Cannot get information from tpl file');
        }
       }
       
       public function display($tpl_file) {
        if(!file_exists($this->templateDir.$tpl_file)) {
            throw new Exception('Template file does not found in '.$this->templateDir.$tpl_file);
        }
        if(file_exists($this->compileDir.'template.'.$tpl_file.'.php')) {
            require_once($this->compileDir.'template.'.$tpl_file.'.php');
            $name = explode(".",$tpl_file);
            $name = 'MD5_FILE_'.$name[0];
            if($$name != md5_file($this->templateDir.$tpl_file)) {
                @unlink($this->compileDir.'template.'.$tpl_file.'.php');
                self::compile($tpl_file);
            }
        } else {
            self::compile($tpl_file);
        }
       }
       
    }
?>